const http = require("http");
const path = require("path");
const fs = require("fs");

http.createServer((req, res) => {
    var urlStr = req.url;
    var filePath = path.join(__dirname, "/index.html");
    var imgPath = path.join(__dirname, "/icon.png");
    var fdirPath = path.join(__dirname, "/1");
    if(urlStr == "/"){
        var fileContent = fs.readFileSync(filePath);
        res.writeHead(200, {"Content-Type": "text.html;charset:utf8"});
        res.write(fileContent);
        res.end();
    }else if(urlStr == "/icon.png"){
        var imgContent = fs.readFileSync(imgPath);
        res.writeHead(200, {"Content-Type": "image/png"});
        res.write(imgContent);
        res.end();
    }else if(urlStr == "/getlist"){
        // var fdirContent = fs.readdirSync(fdirPath);
        var fileObj = fs.statSync(fdirPath);
        var jsonStr = JSON.stringify(fileObj);
        console.log(jsonStr);
        res.writeHead(200, {"Content-Type": "text/plain"});
        res.write(jsonStr);
        res.end();
    }
}).listen(8082);

console.log("Server is listening 8082");